<?php
defined('LMO') or die('Can\'t access !');

class LAccount
{
	private static $instance;
	
	private function __constructor(){}
	
	public static function getInstance()
	{
		if(!isset(self::$instance)) {
			self::$instance = new LAccount();
		}
		return self::$instance;
	}
	
	public function saveAccount($data)
	{
		$f = SERVER_STORAGE_PATH.BS.$data['ip'].SERVER_FILE_EXT;
		return LFile::writeFile($f, trim(json_encode($data)));
	}
	
	public function loadAccount($ip)
	{
		$f = SERVER_STORAGE_PATH.BS.$ip.SERVER_FILE_EXT;
		if(!file_exists($f)) return false;
		$data = trim(LFile::readFile($f));
		return json_decode($data, true);
	}
	
	public function deleteAccount($ip)
	{
		$f = SERVER_STORAGE_PATH.BS.$ip.SERVER_FILE_EXT;
		@unlink($f);
        @unlink(LOG_STORAGE_PATH.BS.$ip.LOG_STATUS);
        @unlink(LOG_STORAGE_PATH.BS.$ip.LOG_MINUTE);
		
		if(file_exists($f)) return false;
		
		return true;
	}
	
	public function addService($ip, $data)
	{
		$account = $this->loadAccount($ip);
		
		if($account == false) return false;
		
		if(empty($account['service'])) return false;
		
		$account['service'][$data['name']] = $data;
		
		return $this->saveAccount($ip, $account);
	}
	
	public function deleteService($ip, $name)
	{
		$account = $this->loadAccount($ip);
		
		if($account == false) return false;
		
		if(empty($account['service'])) return false;
		
		unset($account['service'][$name]);
		
		return $this->saveAccount($ip, $account);
	}
	
	public function loadService($ip)
	{
		$account = $this->loadAccount($ip);
		
		if($account == false) return false;
		
		if(empty($account['service'])) return false;
		
		return $account['service'];
	}
    
    public function checkExist($ip)
    {
        $f = SERVER_STORAGE_PATH.BS.$ip.SERVER_FILE_EXT;
        if(file_exists($f)) {
            return true;
        }
        return false;
    }
}
?>